// pages/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    desc: '',
    time: '',
    notOnload: false
  },

  getDesc() {
    wx.$https('orders.desc', {
      ordersId: this.data.orderId
    }).then(res => {
      this.setData({
        desc: res
      })
      res.autoCloseSurplusSecond == 0 ? "" : this.countDown()
    })
  },

  countDown() {
    let that = this
    that.countDown_()
    setInterval(() => {
      that.countDown_()
    }, 1000)
  },

  countDown_() {
    let desc = this.data.desc
    let time = `${Math.floor(desc.autoCloseSurplusSecond/60)}分钟${desc.autoCloseSurplusSecond%60}秒`
    desc.autoCloseSurplusSecond != 0 && desc.autoCloseSurplusSecond--
    this.setData({
      desc: desc,
      time: time
    })
  },

  delete() {
    let ordersId = this.data.desc.ordersId
    this.confirm.hideShow({
      title: '删除订单',
      info: `确认删除订单吗`,
      args: {
        ope: 'delete',
        ordersId: ordersId
      }
    })
  },

  cancel(e) {
    let ordersId = this.data.desc.ordersId
    this.confirm.hideShow({
      title: '取消订单',
      info: `确认取消订单吗`,
      args: {
        ope: 'cancel',
        ordersId: ordersId
      }
    })
  },

  pay(e) {
    let orderId = this.data.desc.ordersId
    wx.$unifiedorder(orderId, () => {
      wx.showToast({
        title: '支付成功！',
      })
      setTimeout(() => {
        wx.$nav({
          status: 2
        })
      }, 500);
    })
  },

  ope(e) {
    if (e.detail.type == 1) {
      let args = e.detail.args
      if (args.ope == 'cancel') {
        wx.$https('orders.cancel', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '取消成功',
          })
          this.getDesc()
        })
      } else if (args.ope == 'receive') {
        wx.$https('orders.confirm.receive', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '收货成功',
          })
          setTimeout(() => {
            this.getDesc()
          }, 500)
        })
      } else if (args.ope == 'delete') {
        wx.$https('orders.del', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '删除成功',
            mask: true
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 500)
        })
      }
    }
  },

  takeGoods() {
    let ordersId = this.data.desc.ordersId
    this.confirm.hideShow({
      title: '确认收货',
      info: `确认收货吗？`,
      args: {
        ope: 'receive',
        ordersId: ordersId
      }
    })
  },

  navEva() {
    wx.$nav('/pages/evaluate/evaluate', {
      ordersId: this.data.desc.ordersId
    })
  },

  refund() {
    wx.$nav('/pages/refund/refund', {
      ordersId: this.data.desc.ordersId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
    this.getDesc()
    this.setData({
      notOnload: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.confirm = this.selectComponent('#confirm')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.notOnload)
      this.getDesc()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})